package com.starlink.controller.wechat;

import com.starlink.common.WebResponse;
import com.starlink.common.annotation.WebAccess;
import com.github.pagehelper.PageInfo;
import com.starlink.common.annotation.client.weixin.UserPurchaseRes;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.request.SchoolbagOrderRequest;
import com.starlink.model.request.schoolbag.SchoolbagOrderSubmitRequest;
import com.starlink.service.SchoolbagOrderService;
import com.starlink.model.vo.SchoolbagOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author zhanghong
 */
@RestController
@RequestMapping("/wechat/schoolBagOrder")
@Api(value = "【小程序端】书包订单模块", tags = {"【小程序端】书包订单模块"})
public class WechatSchoolbagOrderController {

    @Autowired
    private SchoolbagOrderService schoolbagOrderService;

    @WebAccess(login = true)
    @ApiOperation(value = "根据主键查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/get/{id}")
    public WebResponse<SchoolbagOrderVO> getByKey(HttpServletRequest request, @PathVariable("id") long id) {
        return WebResponse.build(schoolbagOrderService.getByKey(id));
    }

    @WebAccess(login = true)
    @ApiOperation("分页查询")
    @PostMapping("/pageQuery")
    public WebResponse<PageInfo<SchoolbagOrderVO>> pageQuery(@SortDefault(sort = {"create_time"}, direction = Sort.Direction.DESC) Pageable pageable,
                                                           @RequestBody SchoolbagOrderRequest request) {
        return WebResponse.build(schoolbagOrderService.pageQuery(pageable,request));
    }

    @WebAccess(login = true)
    @ApiOperation("书包下单")
    @PostMapping("/payment")
    public WebResponse<UserPurchaseRes> payment(@Valid @RequestBody SchoolbagOrderSubmitRequest request) {
        return WebResponse.build(schoolbagOrderService.payment(request));
    }

    @WebAccess(login = true)
    @ApiOperation("取消订单")
    @PostMapping("/update")
    public WebResponse<Boolean> updateByKey(@Valid @RequestBody SchoolbagOrderRequest request) {
        SchoolbagOrderVO order = schoolbagOrderService.getByKey(request.getId());
        AssertUtil.assertNotNull(order, "订单id不能为空");
        if (order.getOrderStatus() == 1) {
            //删除订单
            schoolbagOrderService.deleteDOByIds(String.valueOf(order.getId()));
            return WebResponse.build(true);
        } else {
            request.setCancelTime(new Date());
            request.setOrderStatus(5);
        }
        return WebResponse.build(schoolbagOrderService.cancelOrder(request));
    }
}
